<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Video extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('video_m');
        $this->load->model('car_m');
    }

    public function index($page = 1)
    {
        $where                      = [];
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->video_m->total();
        $config['base_url']         = base_url('manage/Video/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $video              = $this->video_m->select($where, $start, $limit);
        if ($video) {
            foreach ($video as $key => $val) {
                if(!empty($val['car_category'])){
                    $car =  $this->car_m->find($val['car_category']);
                    $video[$key]['car_category'] = $car['car_name'];
                }else{
                    $video[$key]['car_category'] = "--";
                }

            }
        }
        $data['video'] = $video;
        $this->load->view('video/index', $data);
    }

    public function add()
    {
        $data['pic_path'] = "[]";
        $data['pic_con']  = json_encode([]);
        $data['car']      = $this->car_m->select();
        $this->load->view('video/form', $data);
    }

    public function edit($id = null)
    {
        $video = $this->video_m->find($id);
        if (!$video) {
            show_message('参数错误！', site_url(X_URL . 'Video/index'));
        }
        $del_url = base_url("index.php/Upload/delete_img?url=" . $video['pic_url']);;

        $pic_con[]        = [
            'caption' => "轮播图",
            'size'    => 576237,
            'width'   => '120px',
            'url'     => $del_url,
            "key"     => $video['id'],
        ];
        $data['pic_path'] = "['" . $video['pic_url'] . "']";
        $data['pic_con']  = json_encode($pic_con);

        $data['video'] = $video;
        $data['car']   = $this->car_m->select();
        $this->load->view('video/form', $data);
    }

    public function del($type_id)
    {
        if ($type_id) {
            if ($this->video_m->delete($type_id)) {
                show_message('恭喜，删除成功！', site_url(X_URL . 'Video/index'), 1);
            } else {
                show_message('参数错误！', site_url(X_URL . 'Video/index'));
            }
        } else {
            show_message('参数错误！', site_url(X_URL . 'Video/index'));
        }
    }

    public function update()
    {
        $post                 = $this->input->post();
        $data['title']        = $post['title'];
        $data['pic_url']      = $post['pic_url'] ?? "#";
        $data['link']         = $post['link'] ?? "#";
        $data['car_category'] = $post['car_category'] ?? 0;
        $id                   = $post['id'] ?? 0;
        if ($data['title'] == '') {
            show_message('名称不能为空！', site_url(X_URL . 'Video/index'));
        }
        if ($data['car_category'] == '') {
            show_message('请选择车型！', site_url(X_URL . 'Video/index'));
        }
        if (!$id) {
            $this->video_m->insert($data);
        } else {
            $this->video_m->update(['id' => $id], $data);
        }
        show_message('恭喜，操作成功！', site_url(X_URL . 'Video/index'), 1);
    }
}

